package com.patterns.factory.SingletonFactory;

import java.lang.reflect.Constructor;

/**
 * Created by Xiao Yiyun on 2017/4/14.
 */
public class SingletonFactory {

    private static Singleton1 singleton1;
    private static Singleton2 singleton2;
    private static Singleton3 singleton3;

    static {
        try {
            Class c1 = Class.forName(Singleton1.class.getName());   //获得类名
            Constructor constructor1 = c1.getDeclaredConstructor(); //获得无参构造
            constructor1.setAccessible(true);                       //设置无参构造可访问
            singleton1 = (Singleton1)constructor1.newInstance();    //产生实例对象

            Class c2 = Class.forName(Singleton2.class.getName());
            Constructor constructor2 = c2.getDeclaredConstructor();
            constructor2.setAccessible(true);
            singleton2 = (Singleton2)constructor2.newInstance();

            Class c3 = Class.forName(Singleton3.class.getName());
            Constructor constructor3 = c3.getDeclaredConstructor();
            constructor3.setAccessible(true);
            singleton3 = (Singleton3)constructor3.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Singleton1 getSingleton1() {
        return singleton1;
    }

    public static Singleton2 getSingleton2() {
        return singleton2;
    }

    public static Singleton3 getSingleton3() {
        return singleton3;
    }
}